<script>
$(document).ready(function(){
	//remove icon delete
	var is_admin = "<?php echo $is_admin ?>";
	var user_id = "<?php echo isset($user_info)? $user_info->user_id : "" ?>";
	
	if (user_id != ""){
		if (is_admin !='1'){
			$(".delete_comment").not(".comment_user_"+ user_id).remove();
		}
	}else{
		$(".delete_comment,.link_reply").remove();
	}
	$("#btn_post").click(function(){
		var comment  = $("#txt_comment").val();
		$.ajax({
			url:"<?php echo base_url('comment/post_comment');?>",
			data:{
				'comment':comment
			},
			 type: "POST",
			 dataType :"json",
			success:function(data){
				console.log(data.status);
				if (data.status =="success"){
					var comm_user = "<div class='username_post'>@" + data.user_name + "</div>";
					var comm = "<div class='post_content'>" + comment + "<span style=\"float:right;cursor:pointer;\" class='delete_comment comment_user_" + data.user_id + "' onclick='delete_comment(" + data.comment_id + ")'>x</span></div>";
					var area_detail_comm = '<div id="are_detail_comment_'+ data.comment_id +'" >';
					var comm_reply = "<div class='link_reply' id='reply_comment_"+data.comment_id+"' onclick='open_reply(this)' >reply</div>";

					var comm_reply_edit = '<div  style="display:none;" id="area_reply_comment_'+ data.comment_id +'" >';
					comm_reply_edit += '<textarea id="txt_comment_reply_'+ data.comment_id +'"> </textarea>';
					comm_reply_edit += '<span class="link_reply" id="btn_comment_reply_'+ data.comment_id +'" onclick="post_reply(this)" >post</span>';
					comm_reply_edit += '<span class="link_reply" id="btn_cancel_comment_reply_'+ data.comment_id +'" onclick="post_reply_cancel(this)" >cancel</span>';
					comm_reply_edit += '</div>';
					$("#view_comments").append('<div id="comment_' + data.comment_id + '">' + comm_user + comm  + area_detail_comm+ comm_reply_edit +comm_reply + '</div>');


					//remove text
					$("#txt_comment").val('');
				}
				
			}
		});
	});
});
function open_reply(obj){
	var id= obj.id;
	var id_comment = id.substr(id.lastIndexOf("_") + 1,id.lenght);
	$("#area_reply_comment_"+id_comment).show();
}
function post_reply_cancel(obj){
	var id= obj.id;
	var id_comment = id.substr(id.lastIndexOf("_") + 1,id.lenght);
	$("#area_reply_comment_"+id_comment).hide();
}
function post_reply(obj){
	var id= obj.id;
	var id_comment = id.substr(id.lastIndexOf("_") + 1,id.lenght);
	
	var comment  = $("#txt_comment_reply_"+id_comment).val();
	$.ajax({
		url:"<?php echo base_url('user/post_comment_detail');?>",
		data:{
			'comment':comment,
			'comment_id':id_comment
		},
		 type: "POST",
		 dataType :"json",
		success:function(data){
			if (data.status =="success"){
				var comm_user = "<div class='username_post_detail comment_detail_" + data.comment_detail_id +  "'>@" + data.user_name + "</div>";
				var comm = "<div class='post_content_detail comment_detail_" + data.comment_detail_id +  "' >" + comment + "<span style=\"float:right;cursor:pointer;\" class='delete_comment comment_user_" + data.user_id + "' onclick='delete_comment_detail(" + data.comment_detail_id + ")'>x</span></div>";
				$("#are_detail_comment_"+id_comment).append(comm_user + comm);

				//remove text
				$("#txt_comment_reply_"+id_comment).val('')
			}
			
		}
	});
	
	$("#area_reply_comment_"+id_comment).hide();
}
function delete_comment(id){
	if (confirm("<?php echo lang('confirm_delete')?>")){
		
		$("#comment_"+id).hide();
		$.ajax({
			url:"<?php echo base_url('comment/delete_comment');?>",
			data:{
				'comment_id':id
			},
			 type: "POST",
			 dataType :"json",
			success:function(data){
			}
		});
	}
}
function delete_comment_detail(id){
	if (confirm("<?php echo lang('confirm_delete')?>")){
		$(".comment_detail_"+id).hide();
		$.ajax({
			url:"<?php echo base_url('user/delete_comment_detail');?>",
			data:{
				'comment_detail_id':id
			},
			 type: "POST",
			 dataType :"json",
			success:function(data){
			}
		});
	}
}
</script>


<?php if ( isset($user_info) ) {?>
	<div>
		<textarea id="txt_comment"> </textarea>
	</div>
	<input type="button" id="btn_post" value="post"></input>
<?php }?>
<div class="title_right border-right"  ><?php echo lang('comment')?></div>
<div id="view_comments" class="border-right">
	<?php foreach ($comments as $item ) {?>
	<div id="comment_<?php echo $item['comment']->id?>">
		<div class="username_post">@<?php echo $item['comment'] -> username?></div>
		<div class="post_content"><?php echo $item['comment']->comment ?><span style="float:right;cursor:pointer;" class="delete_comment comment_user_<?php echo $item['comment']->user_id?>" onclick="delete_comment(<?php echo $item['comment']->id?>)">x</span></div>
		<div id="are_detail_comment_<?php echo $item['comment']->id?>" >
		<!--  detail -->
		
			<?php foreach ($item['comment_detail'] as $item_detail ) {?>
				<div class="username_post_detail comment_detail_<?php echo $item_detail->comment_detail_id?>">@<?php echo $item_detail-> username?></div>
				<div class="post_content_detail comment_detail_<?php echo $item_detail->comment_detail_id?>" ><?php echo $item_detail->comment_detail ?><span style="float:right;cursor:pointer;" class="delete_comment comment_user_<?php echo $item_detail->user_id?>" onclick="delete_comment_detail(<?php echo $item_detail->comment_detail_id?>)">x</span></div>
			<?php }?>
		<!--  detail end -->
		</div>
		<div class="link_reply" id="reply_comment_<?php echo $item['comment']->id?>" onclick="open_reply(this)">replay</div>
		<div  style="display:none;" id="area_reply_comment_<?php echo $item['comment']->id?>" >
			<textarea id="txt_comment_reply_<?php echo $item['comment']->id?>"> </textarea>
			<span class="link_reply" id="btn_comment_reply_<?php echo $item['comment']->id?>" onclick="post_reply(this)" >post</span>
			<span class="link_reply" id="btn_cancel_comment_reply_<?php echo $item['comment']->id?>" onclick="post_reply_cancel(this)" >cancel</span>
		</div>
	</div>
	<?php }?>
</div>